var BSTMG = BSTMG || {};
BSTMG.companyManager = {
	init: function(){
		var that = this;
		//分页功能
		that.selectCompanyList("","");
		//右键菜单功能
		that.rightMenu();
		//根据状态查询
		that.isLive();
		//根据登录号查询
		that.query();
	},
	selectCompanyList: function(isLive,userName){
		var url = "/sysAdmin/userAdmin/companyList/search?isLive="+isLive+"&userName="+userName;
		BSTMG.common.hjhPages(url,function(data){
			var companyList = data.companyList;
			var countArr = data.countArr.split('|');
			var $tbody = $("#companyList tbody");
			var $tr = '';
			if(companyList.length > 0) {
				for(var i=0;i<companyList.length;i++){
					var companyInfo = companyList[i];
					var loginNum = setLoginNum(companyInfo);
					var registerTime = toDate(companyInfo.addTime, "yyyy-MM-dd hh:mm:ss");
					var userValid = setUserValid(companyInfo);
					var accountMoney = setAccountMoney(companyInfo);
					$tr += '<tr class="odd">';
					$tr += '<td data-id="'+companyInfo.userId+'" data-userValid="'+companyInfo.userValid+'" data-domain="'+companyInfo.companyDomainName+'">'+(i+1)+'</td>';
					$tr += '<td>'+BSTMG.tools.checkNull(loginNum)+'</td>';
					$tr += '<td>'+BSTMG.tools.checkNull(toDate(companyInfo.addTime, "yyyy-MM-dd hh:mm:ss"))+'</td>';
					//$tr += '<td>'+BSTMG.tools.checkNull(toDate(companyInfo.overTime, "yyyy-MM-dd hh:mm:ss"))+'</td>';
					$tr += '<td>'+BSTMG.tools.checkNull(companyInfo.contactName)+'</td>';
					$tr += '<td>'+parseFloat(accountMoney).toFixed(2)+'</td>';
					$tr += '<td>'+BSTMG.tools.checkNull(companyInfo.belongCompanyName)+'</td>';
					$tr += '<td>'+countArr[i]+'</td>';
					$tr += '<td>'+BSTMG.tools.checkNull(userValid)+'</td>';
					if(companyInfo.isLive==0){
						if(companyInfo.userValid == 0){
							$tr += '<td><a type="button"  class="btn btn-danger" onclick="deleteCompany('+companyInfo.userId+');">删除</a></td>';
						}else if(companyInfo.userValid == 1){
							$tr += '<td><a type="button"  class="btn btn-danger disabled" onclick="deleteCompany('+companyInfo.userId+');">删除</a></td>';
						}
					}else if(companyInfo.isLive==1){
						if(companyInfo.userValid == 0){
							$tr += '<td><a type="button" class="btn btn-primary" onclick="doUser('+companyInfo.userId+',false);">停用</a></td>';
						}else if(companyInfo.userValid == 1){
							$tr += '<td><a type="button" class="btn btn-success" onclick="doUser('+companyInfo.userId+',true);">启用</a></td>';
						}
					}
					$tr += '</td></tr>';
				}
				$tbody.html($tr);
			}else {
				var $tr = '<tr class="odd"><td colspan="9">暂无数据！</td></tr>';
				$tbody.html($tr);
			}
		});
	},
	isLive : function(){
		$('#isLive').on('change',function(){
			var isLive = $('#isLive').val();
			BSTMG.companyManager.selectCompanyList(isLive,"");
		})
	},
	query : function(){
		$('#query').on('click',function(){
			var userName = $('#userName').val();
			BSTMG.companyManager.selectCompanyList("",userName);
		})
	},
	
	rightMenu: function(){
		var $companyList = $("#companyList");
		BSTMG.plugins.kittyMenu();
		$companyList.kittyMenu([{
			"menuname":"查看详情",
			"className": "btn-info",
			"clickaction": function(event){	 //此事件为当前右键菜单事件
				var $this = $(event.target).parent("tr");		//当前选中的行
				var userId = $this.find("td").eq(0).attr("data-id");
				location.href = "/sysAdmin/userAdmin/companyDetail/"+userId;
			}
		},{
			"menuname":"员工清单",
			"className": "btn-danger",
			"clickaction": function(event){
				var $this = $(event.target).parent("tr");		//当前选中的行
				var userId = $this.find("td").eq(0).attr("data-id");
				location.href = "/sysAdmin/userAdmin/companyEmpList/"+userId;
			}
		}]);
		// 清除第一行右键菜单功能呢过
		$companyList.find("tr:first-child").on("contextmenu",function(){
			return false;
		});
		
	}
}

$(function(){
	BSTMG.companyManager.init();
});
//回车键查询功能
function keyQuery(){
	if(event.keyCode == 13){
		$('#query').trigger("click");
	}
}
//设置登陆账号
function setLoginNum(companyInfo){
	if(companyInfo.userEmail != "" && companyInfo.userEmail != null){
		return companyInfo.userEmail;
	}else if(companyInfo.userTel != "" && companyInfo.userTel != null){
		return companyInfo.userTel;
	}else{
		return "";
	}
}

function setAccountMoney(companyInfo){
	if(companyInfo.accountMoney != "" && companyInfo.accountMoney != null){
		return companyInfo.accountMoney;
	}else{
		return 0.00;
	}
}
//设置是否有效
function setUserValid(companyInfo){
	if(companyInfo.isLive==0){
		if(companyInfo.userValid ==0){
			return "未激活";
		}else if(companyInfo.userValid == 1){
			return "已删除";
		}
	}else if(companyInfo.isLive==1){
		if(companyInfo.userValid ==0){
			return "有效";
		}else if(companyInfo.userValid == 1){
			return "无效";
		}else{
			return "";
		}
	}
}
//停用或者启用用户操作
function doUser (userId,flag){
	var confirmContent = "";
	if(flag) {
		confirmContent = "确定要启用此用户吗？";
	}else {
		confirmContent = "确定要停用此用户吗？";
	}
	
	BSTYUN.dialog.confirm(confirmContent,function(){
		//var isLive = $('#isLive').val();
		$.ajax({
			type : "POST",
			url : "/sysAdmin/operate/userAdmin/companyUpdateUserValid",
			async : false,
			data:{
				userId:userId,
				//isLive:isLive,
				flag : flag
			},
			success:function(data){
				if(data.isSuccess){
					var isLive = $('#isLive').val();
					BSTMG.companyManager.selectCompanyList(isLive,"");
				}else {
					BSTYUN.dialog.warn(data.message);
				}
			}
		})
	});
}
//删除未激活用户
function deleteCompany(userId){
	BSTYUN.dialog.confirm("确定要删除此用户吗？",function(){
		$.ajax({
			type : "POST",
			url : "/sysAdmin/operate/userAdmin/deleteCompany",
			async : false,
			data:{
				userId:userId,
			},
			success:function(data){
				if(data.isSuccess){
					BSTYUN.dialog.warn(data.message);
					var isLive = $('#isLive').val();
					BSTMG.companyManager.selectCompanyList(isLive,"");
				}else {
					BSTYUN.dialog.warn(data.message);
				}
			},
			error: function(){
				BSTYUN.dialog.warn("网络错误！");
			}
		})
	});
}



